#include "getinfo.h"
#include <QFile>
#include <QChar>
#include <qdebug.h>
char*** getUsrInfo(int* size)
{
    QFile file("E:\\Qt\\BookStore\\usrinfo.txt");
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        qDebug()<<"打开失败";
        return NULL;
    }
    QByteArray line = file.readAll();
    (*size)=getNumofUsrs(line);
    char *tmp = line.data();
    char*** usr=(char***)malloc(size[0]*sizeof(char**));
    int index1=0;
    for(int i=0;i<size[0];i++)
    {
        int index2=0;
        usr[i]=(char**)malloc(2*sizeof(char*));
        usr[i][0]=(char*)malloc(16*sizeof(char));
        usr[i][1]=(char*)malloc(16*sizeof(char));
        while(tmp[index1]!=' ')
        {
            usr[i][0][index2++]=tmp[index1++];
        }
        usr[i][0][index2]=0;
        index1++;index2=0;
        while(tmp[index1]!='\n'&&tmp[index1])
        {
            usr[i][1][index2++]=tmp[index1++];
        }
        usr[i][1][index2]=0;index1++;
    }
    return usr;
}
int getNumofUsrs(QByteArray arr)
{
    int sum=0;
    char *usr = arr.data();
    while(*usr)
    {
        if(*usr=='\n')
        {
            sum++;
        }
        usr++;
    }
    return sum+1;
}

char* qstring2Char(QString str)
{
    char* arr=(char*)malloc(str.length()*sizeof(char));
    for(int i=0;i<str.length();i++)
    {
        arr[i]=str.at(i).unicode();
    }
    arr[str.length()]='\0';
    return arr;
}

int strcmp(char* str1,char* str2)
{
    while(*str1)
    {
        if(*str1==*str2)
        {
            str1++;str2++;
        }
        else
        {
            return 1;
        }
    }
    if(*str2)
        return 1;
    return 0;
}
